/************************************************************************
*************************************************************************
*************************************************************************

prodestcon_testtable.do

Makes the table of results from the specification test, and the widstat test.

You need to set the flags below (search "Run Tests") to tell the code which
tests (if any) to recalculate. Otherwise it just makes the tables.

*************************************************************************
*************************************************************************
************************************************************************/

//Load sharetest
do "do/programs/sharetest.ado"

//Load widtests
do "do/programs/rkstat_functions.do"



//Degree of control function for main empirical table
global deg = 4

/*****************************************************************
******************************************************************
Helper programs
******************************************************************
*****************************************************************/

/*****************************************************************
Product
*****************************************************************/

//Recursive program to get the pairwise products of a set of terms
cap program drop product
program product
	syntax varlist
	
	gettoken firstvar resvars : varlist	
		
	//Take the product
	foreach var of varlist `varlist' {
		gen `firstvar'X`var' = `firstvar'*`var'
	}
	
	//If we're not on the last word, keep going
	if !mi(word("`resvars'",1)) {
		//Continue recursion
		product `resvars'
	}
end




/*****************************************************************
Formdata: Chile
*****************************************************************/
/*
This program simply applies the code given to me by Salvador Navarro
to form the Chilean data.

The only argument is the sector we want to keep
*/


cap program drop formdata
program formdata
		args ind
		
		
		keep if ciiu_3d==`ind'

		gen adv=.
		replace adv=0 if adverts==0 & adverts~=.
		replace adv=1 if adverts>0 & adverts~=.

		gen skwages=wageswc+ bonuswc+ prtaxwc+ fataxwc
		replace skwages=. if skwages<=0
		gen unskwages=wagesbc+ bonusbc+ prtaxbc+ fataxbc
		replace unskwages=. if unskwages<=0
		gen totwages=skwages+unskwages
		replace totwages=. if totwages<=0
		gen ratiowcbc = whitelab / bluelab
		replace ratiowcbc = 10000 if ratiowcbc == . & bluelab == 0 & whitelab ~= 0

		gen output= salegds + shipgds + resales - goodspch + contract + conrepai + coninsta + prodbld + prodmach + prodveh + elecsval + finvufgd + finvfgds - iinvufgd - iinvfgds
		replace output=. if output<=0
		gen matls = totrawma - finvrm + iinvrm + watival + watdval + greasval + lubeval
		replace matls=. if matls<=0
		replace realmats=. if realmats<=0
		gen energ = coalval + carbval +cokeval + petrval + diesval + benzval + parafval + lgasval + pgasval + fwoodval + ofuelval + elecbval
		replace energ=. if energ<=0 
		replace renerg=. if renerg<=0 
		gen serv = cstwtp+freight+insuran+communic+techsupp+accnting+inhouse+salescom+othserv
		replace serv=. if serv<=0
		replace rserv=. if rserv<=0

		gen GO=output/10
		gen RGO=routput/10
		replace RGO=. if RGO<=0

		gen VA=valadded/10
		gen RVA=rva/10
		drop rva
		replace RVA=. if RVA<=0

		gen WI=(energ+matls+serv)/10
		gen RI=(renerg+realmats+rserv)/10
		gen RN=renerg/10
		gen RV=rserv/10
		gen RM=realmats/10
		gen RMV=RM+RV
		gen RNV=RN+RV
		gen WN=energ/10
		gen WV=serv/10
		gen WM=matls/10
		gen WMV=WM+WV
		gen WNV=WN+WV

		gen L = totlab
		gen WL = totwages/10
		gen WLS= skwages/10
		gen WLU = unskwages/10


		gen cpi1985=cpi/16.6436
		gen RL=WL/cpi85
		gen RLS=WLS/cpi85
		gen RLU=WLU/cpi85

		gen K = rcapstock/10
		replace K=. if K<=0

		gen netinv = rinvest/10
		replace netinv=. if netinv<=0
		gen nominv=invnbld + invnmach + invnveh + invubld + invumach + invuveh - salesbld -salesuma - salesuve + impbld + impmach + impveh + prodbld + prodmach + prodveh
		replace nominv=nominv/10
		replace nominv=. if nominv<=0

		gen si = log(WI/GO)
		gen sl = log(WL/GO)
		gen sn = log(WN/GO)
		gen snv = log(WNV/GO)

		replace defoutput=defoutput/100

		//=================================================================================================================================================
		//=================================================================================================================================================
		// Specific to each industry
		//=================================================================================================================================================
		//=================================================================================================================================================
		//replace si=. if si>1.5
		//replace K=. if K>2500
		//=================================================================================================================================================
		//=================================================================================================================================================
		//=================================================================================================================================================
		//=================================================================================================================================================

		gen export=.
		replace export=0 if exports~=. & year>1989
		replace export=1 if exports~=. & exports>0
		gen import=.
		replace import=0 if rawmatsi~=. & rawmatsi==0
		replace import=1 if rawmatsi~=. & rawmatsi>0

		gen wages=.
		replace wages=totwages/L
		bys year: egen mm=median(wages) if wages~=.
		gen hiwag=.
		replace hiwag=0 if wages<mm & wages!=.
		replace hiwag=1 if wages>mm & wages!=.
		drop mm

		keep id year GO RGO VA RVA RN WN RV WV RM WM RMV WMV RNV WNV RI WI RL WL RLS WLS RLU WLU K netinv nominv L si sl sn snv defoutput export import hiwag adv ratiowcbc ///
				faland stassets XX152 XX147 XX148 XX149 XX150 XX151 netprof 

		reshape wide GO RGO VA RVA RN WN RV WV RM WM RMV WMV RNV WNV RI WI RL WL RLS WLS RLU WLU K netinv nominv L si sl sn snv defoutput export import hiwag adv ratiowcbc faland stassets XX152 XX147 XX148 XX149 XX150 XX151 netprof , i(id) j(year)
		reshape long GO RGO VA RVA RN WN RV WV RM WM RMV WMV RNV WNV RI WI RL WL RLS WLS RLU WLU K netinv nominv L si sl sn snv defoutput export import hiwag adv ratiowcbc faland stassets XX152 XX147 XX148 XX149 XX150 XX151 netprof , i(id) j(year)

		bys year: egen def=mean(defoutput)
		drop defoutput

		rename netinv INV

		foreach i of varlist GO RGO VA RVA RN RV RM RMV RNV RI RL RLS RLU K INV nominv L def{
			gen log`i'=log(`i')
		}	

		compress
		sort id year
		save miss_data_jan_28_2011, replace

		bys year: egen tot=sum(RGO)
		gen s=RGO/tot
		gen temp=s*ln(RGO)
		bys year: egen q_index=sum(temp)
		drop temp
		gen temp=s*RGO
		bys year: egen q_index_l=sum(temp)
		drop tot s temp

		foreach i of varlist GO RGO VA RVA RN RV RM RMV RNV RI RL RLS RLU K INV nominv L def{
			replace log`i'=-1000.0 if `i'==. | `i'<=0
		}
		 
		foreach i of varlist GO RGO VA RVA RN WN RV WV RM WM RMV WMV RNV WNV RI WI RL WL RLS WLS RLU WLU K INV nominv L {
			gen I`i'=1
			replace I`i'=0 if `i'==. | `i'<=0
			replace `i'=-1000.0 if I`i'==0
		}	

		foreach i of varlist si sl sn snv {
			gen I`i'=1
			replace I`i'=0 if `i'==.
			replace `i'=-1000.0 if I`i'==0
		}

		replace export=-1000 if export==.
		replace import=-1000 if import==.
		bys year: egen mm=median(logK) if logK>-900
		gen sizek=.
		replace sizek=0 if logK<mm & logK>-999
		replace sizek=1 if logK>=mm
		drop mm
		replace sizek=-1000 if sizek==.

		bys year: egen mm=median(ratiowcbc) 
		gen wcbc=.
		replace wcbc=0 if ratiowcbc<=mm
		replace wcbc=1 if ratiowcbc>mm
		drop mm ratiowcbc
		replace wcbc=-1000 if wcbc==.

		gen l_q_index=ln(q_index)
		gen l_q_index_l=ln(q_index_l)
		drop def logdef q_index q_index_l

		replace hiwag=-1000 if hiwag==.
		replace adv=-1000 if adv==.

		#delimit;
		order GO logGO IGO RGO logRGO IRGO VA logVA IVA RVA logRVA IRVA si Isi sl Isl sn Isn snv Isnv RN logRN IRN WN IWN RV logRV IRV WV IWV 
			RM logRM IRM WM IWM RMV logRMV IRMV WMV IWMV RNV logRNV IRNV WNV IWNV
			RI logRI IRI WI IWI RL logRL IRL WL IWL 
			RLS logRLS IRLS WLS IWLS RLU logRLU IRLU WLU IWLU K logK IK id year 
			INV logINV IINV nominv lognominv Inominv L logL IL l_q_index l_q_index_l 
			export import sizek hiwag adv wcbc; 
		#delimit cr
		sort id year
		compress
		
		//This command is my own; I don't know why log(0) was set to -1000, but I undo it here;
		//since this has to be done to match GNR's OLS results, I'm pretty sure they do the same thing
		recode logL logK logRI logRGO (-1000 = .)

end


/*****************************************************************
Prepare Colombia data for analysis
*****************************************************************/

/***********
The following code is taken from the files sent by Salvador Navarro;
The only change is to keep n1 (inventory of raw materials at the 
beginning of the year).
***********/ 
cap program drop colraw
program colraw
	/*   ----------------Begin Outside Code---------------------      */
	use "dta/mergecap.dta", clear

	rename n5 savk
	rename x3 savk2


	xtset plant datayear
	//egen totexp = rowtotal(w7 w14 e5 c7 c17 i5 i11  t6 t1 t2 t3 t4 t5 t6 )
	//egen totrev = rowtotal(s5 t9 s9)
	//gen savk = l.totrev - l.totexp
	//sort plant datayear
	//bysort plant: gen prodyears = _n-1

	//gen yrop = x6 if x6 < 91
	//gen savk = datayear - yrop
	//replace savk = . if savk < 0

	foreach var of varlist p_machin p_struc p_transp p_gdp {
		egen mep= mean(`var'), by (datayear)
		gen tt= mep if datayear==81
		egen ep_81= max(tt)
		gen epindex= `var'/ep_81
		rename `var' `var'_old
		rename epindex `var'
		drop tt ep_81 mep 
	}	

	gen import_inp=.
	replace import_inp=0 if s7==0
	replace import_inp=1 if s7>0 & s7!=.
	gen import_mat=.
	replace import_mat=0 if s11==0
	replace import_mat=1 if s11>0 & s11!=.

	gen adv=.
	replace adv=0 if c13==0 & c13~=.
	replace adv=1 if c13>0 & c13~=.

	replace rland_80=rland_80*p_struc_old/p_struc
	replace rbldg_80=rbldg_80*p_struc_old/p_struc
	replace rmach_80=rmach_80*p_machin_old/p_machin
	replace rtrans_80=rtrans_80*p_transp_old/p_transp
	replace roffice_80=roffice_80*p_machin_old/p_machin
	local i=80
	replace rcap`i'=rland_`i'+ rbldg_`i'+ rmach_`i'+ rtrans_`i'+ roffice_`i'
	replace rcap`i'=. if rcap`i'<= 0

	/*** PRICE INDEX FOR ENERGY INPUTS ***/

	/*** USING QUANTITY AND VALUE OF ENERGY CONSUMED ***/
	/*** AVAILABLE FROM 1981 ***/

	gen epr= e7/ e4
	egen mepr= mean(epr), by (datayear)
	gen tt= mepr if datayear==81
	egen epr_81= max(tt)
	drop tt
	gen eprindex= epr/epr_81
	label var eprindex "price index of energy (consumed, base 1981)"

	/*** TOTAL ENERGY INPUT (REAL VALUE) ***/

	gen renergy= e7/eprindex 
	label var renergy "REAL ENERGY VALUE CONSUMED AT 1981 VALUE"

	drop if datayear<81

	gen split = s3/s5
	replace split = . if split>1
	gen l_split = ln(split)

	gen go = pg
	label var go "nominal gross ouput"
	gen gd = split * pg
	label var gd "nominal gross domestic ouput"
	gen ge = (1-split) * pg
	label var ge "nominal gross exports ouput"

	*replace p_output = pg / rp if p_output == .
	*gen rgo = go / p_output
	*gen rgd = gd / p_output
	*gen rge = ge / p_output

	gen rgo = go / p_gdp
	label var rgo "real gross ouput, base 1981"
	gen rgd = gd / p_gdp
	label var rgd "real gross domestic ouput, base 1981"
	gen rge = ge / p_gdp
	label var rge "real gross exports ouput, base 1981"

	gen rk = rcap80
	replace rk=. if rk<=0
	label var rk "real capital stock, base 1981"
	gen rinv= (inv_land/p_struc) + (inv_bldg/p_struc) + (inv_mach/p_machin) + (inv_trans/p_transp) + (inv_office/p_machin)
	replace rinv=. if rinv<=0
	label var rinv "real investment, base 1981"

	gen l = sklab*(skwages/sklab)/(unskwages/unsklab) + unsklab
	label var l "labor (employees)"
	gen yl = l*x7/12 
	drop l
	rename yl l
	label var l "labor (employee years)"

	gen ratiowcbc = sklab/unsklab
	label var ratiowcbc "ratio wc bc workers"

	gen wl = skwages + unskwages
	label var wl "nominal wage bill"
	gen rwl = wl / p_gdp
	label var rwl "real wage bill, base 1981"
	ren s10 mats
	label var mats "nominal materials"
	rename servpur serv
	label var serv "nominal services"
	rename e7 energy
	label var energy "nominal energy consumed"
	gen ii = mats + serv + energy
	label var ii "nominal intermdiate inputs (mats+serv+energ)"
	replace rmatused=rmatused*p_gdp_old/p_gdp
	rename rmatused rmats
	label var rmats "real materials, base 1981"
	replace rserv=rserv*p_gdp_old/p_gdp
	label var rserv "real services, base 1981"
	gen rii = rmats + rserv + renergy
	label var rii "real intermdiate inputs, base 1981"
	gen iic = ic
	label var iic "intermediate consumption"
	gen riic = ic/p_gdp
	label var riic "real intermediate consumption, base 1981"

	gen ms = mats + serv
	label var ms "nominal mats+serv"
	gen rms = rmats + rserv
	label var rms "real mats+serv, base 1981"

	gen sii=ii/go
	label var sii "share int inputs"
	gen sn=energy/go
	label var sn "share energy"
	gen sl=wl/go
	label var sl "share labor"

	drop val* rva va*

	gen va=go-ii
	replace va=. if va<=0
	label var va "nominal value added"
	gen rva=rgo-rii
	replace rva=. if rva<=0
	label var rva "real value added, base 1981"

	gen vams=go-ms
	replace vams=. if vams<=0
	label var va "nominal value added mats+serv"
	gen rvams=rgo-rms
	replace rvams=. if rvams<=0
	label var rvams "real value added mate+serv, base 1981"

	gen sn_vams=energy/vams
	label var sn_vams "share energy vams" 
	gen sl_va=wl/vams
	label var sl_va "share labor va"

	rename datayear year

	gen d_exp = s4> 0 & s4~= .

	gen switchflag = 0
	sort plant year
	bys plant: replace switchflag = 4 if sic[_n] ~= sic[_n-1] & sic[_n-1] ~= .
	sort plant year
	bys plant: replace switchflag = 3 if sic3[_n] ~= sic3[_n-1] & sic3[_n-1] ~= .
	label var switchflag "Flag for Sector Switch in History of Establishment"

	#delimit;
	keep plant year go gd ge rgo rgd rge p_gdp va rva vams rvams sii sn sl sn_vams sl_va l wl rwl mats rmats savk savk2
		energy renergy serv rserv rk ii rii ms rms d_exp sic3 rinv switchflag import_inp import_mat adv ratiowcbc;
	order plant year go rgo gd rgd ge rge p_gdp va rva vams rvams sii sn sl sn_vams sl_va l wl rwl mats rmats 
		energy renergy serv rserv rk ii rii ms rms d_exp sic3 rinv switchflag import_inp import_mat adv ratiowcbc;

	reshape wide go rgo gd rgd ge rge va rva vams rvams sii sn sl sn_vams sl_va l wl rwl mats rmats savk savk2
		energy renergy serv rserv rk ii rii ms rms d_exp p_gdp rinv switchflag import_inp import_mat adv ratiowcbc, i(plant sic3) j(year);
	reshape long go rgo gd rgd ge rge va rva vams rvams sii sn sl sn_vams sl_va l wl rwl mats rmats savk savk2
		energy renergy serv rserv rk ii rii ms rms d_exp p_gdp rinv switchflag import_inp import_mat adv ratiowcbc, i(plant sic3) j(year);
	#delimit cr
	bys year: egen a=mean(p_gdp)
	replace p_gdp=a if p_gdp==.
	drop a

	foreach var of varlist go-rms rinv sic3 {
		replace `var'=. if `var'==0
		gen log`var'=ln(`var')
		gen I`var'=1
		replace I`var'=0 if log`var'==.
	}

	gen Id_exp=1
	replace Id_exp=0 if d_exp==.
	gen Iimport_inp=1
	replace Iimport_inp=0 if import_inp==.
	gen Iimport_mat=1
	replace Iimport_mat=0 if import_mat==.

	drop logp_gdp Ip_gdp logsic3 Isic3

	#delimit;
	order plant year go loggo Igo rgo logrgo Irgo gd loggd Igd rgd logrgd Irgd ge logge Ige rge logrge Irge d_exp Id_exp 
		p_gdp va logva Iva rva logrva Irva vams logvams Ivams rvams logrvams Irvams sii logsii Isii sn logsn Isn 
		sl logsl Isl sn_vams logsn_vams Isn_vams sl_va logsl_va Isl_va l logl Il wl logwl Iwl rwl logrwl Irwl 
		mats logmats Imats rmats logrmats Irmats energy logenergy Ienergy renergy logrenergy Irenergy serv logserv Iserv
		rserv logrserv Irserv rk logrk Irk ii logii Iii rii logrii Irii ms logms Ims rms logrms Irms rinv logrinv Irinv 
		sic3 import_inp import_mat adv ratiowcbc;
	#delimit cr	 

	compress
	sort plant year
end
/*   ----------------End Outside Code---------------------      */





/*****************************************************************
formdatacol: Columbia
*****************************************************************/
/*
This program simply applies the code given to me by Salvador Navarro
to form the Colombian data.

The only argument is the sector we want to keep raw materials in inventory
*/


cap program drop formdatacol
program formdatacol
		args ind
		

		keep if sic3==`ind'

		gen wages=wl/l
		bys year: egen mm=median(wages) if wages~=.
		gen hiwag=.
		replace hiwag=0 if wages<=mm & wages~=.
		replace hiwag=1 if wages>mm & wages~=.
		drop mm


		/*
		bys year: egen mm=median(ratiowcbc) 
		gen wcbc=.
		replace wcbc=0 if ratiowcbc<=mm 
		replace wcbc=1 if ratiowcbc>mm 
		drop mm ratiowcbc
		*/

		ren d_exp export
		gen import=0
		replace import=1 if import_inp==1 | import_mat==1
		replace import=. if (import_inp==. | import_mat==.) & import==0
		gen K=exp(logrk)
		drop logrk
		gen logrk=log(K)
		drop K

		keep plant year logrgo rgo Irgo logsii Isii logrva rva Irva logl l Il logrk rk Irk logrii rii Irii export import savk savk2 //adv hiwag wcbc 
		ren plant id
		ren logrgo logRGO
		ren rgo RGO 
		ren Irgo IRGO
		ren logsii si
		ren Isii Isi
		ren logrva logRVA
		ren rva RVA 
		ren Irva IRVA
		ren logl logL
		ren l L 
		ren Il IL
		ren logrk logK
		ren rk K
		ren Irk IK
		ren logrii logRI
		ren rii RI
		ren Irii IRI
		bys year: egen mm=median(logK) 
		gen sizek=.
		replace sizek=0 if logK<mm & logK~=.
		replace sizek=1 if logK>=mm & logK~=.
		drop mm

		scalar scale = 10
		replace logRGO = logRGO - ln(scale)
		replace RGO = RGO / scale
		replace logRVA = logRVA - ln(scale)
		replace RVA = RVA / scale
		replace logK = logK - ln(scale)
		replace K = K / scale
		replace logRI = logRI - ln(scale)
		replace RI = RI / scale


		#delimit;
		order logRGO IRGO si Isi logRI IRI logK IK id year 
			logL IL export import sizek   ;  //adv hiwag wcbc
		#delimit cr
		sort id year
		compress
		
		//This command is my own; I don't know why log(0) was set to -1000, but I undo it here;
		//since this has to be done to match GNR's OLS results, I'm pretty sure they do the same thing
		recode logL logK logRI logRGO (-1000 = .)

end



/*****************************************************************
Thailand Data
*****************************************************************/
cap program drop formdatathai
program formdatathai
	//The variable uniquely identifying households
	global idvar caseid

	/*****************************************************************
	Deflators
	*****************************************************************/

	global def1996 = 0.76883974
	global def1997 = 0.812093106
	global def1998 = 0.877017747
	global def1999 = 0.879514849
	global def2000 = 0.893516454
	global def2001 = 0.908053153
	global def2002 = 0.914385089
	global def2003 = 0.930883796
	global def2004 = 0.956568269
	global def2005 = 1            //Note that deflator statements from survey year 2006 will report 0 real changes made
	global def2006 = 1.046374744
	global def2007 = 1.069829662
	global def2008 = 1.128333185
	global def2009 = 1.118790689

	/*****************************************************************
	Land Value
	*****************************************************************/


	/*****************************
	Step 1: land used by household
	*****************************/
	//Loop through each year
	foreach year of numlist 1997/2009 {
		if `year' == 1997 {
			use "${data}`year'/hh/j20tb1.dta", clear
		}
		else if `year' == 1998 {
			use "${data}`year'/hh/la_t1.dta", clear
		}
		else if `year' == 1999 {
			use "${data}`year'/hh/la6.dta", clear
		}
		else {
			local yr = mod(`year',100)
			use "${data}`year'/hh/hh0`yr'_14la_tab1.dta", clear
		}
		
		//Keep only owned land
		keep if la8 == 1

		
		//Generate two measures of land value: value of all owned land, and value of
		//non-rice land
		replace la14 = . if la14 < 0
		bysort ${idvar}: egen us_landval_all 		= total(la14)
		bysort ${idvar}: egen us_landval_nonrice 	= total(la14) if la7 != 5
		
		collapse (median) us_landval*, by(${idvar})
		gen year = `year' - 1
		
		tempfile `year'
		save ``year''
	}

	use `1997'
	foreach year of numlist 1998/2009 {
		append using ``year''
	}

	//These people may farm rice but don't own any non-rice land; set their non-rice
	//land value equal to zero
	replace us_landval_nonrice = 0 if mi(us_landval_nonrice) & !mi(us_landval_all)

	tempfile landvaltmp
	save `landvaltmp'



	/*****************************
	Step 2: land not used by owner
	*****************************/

	//This is land the household rents out, etc.
	foreach year of numlist 1997/2009 {
		if `year' == 1997 {
			use "${data}`year'/hh/j20tb2.dta", clear
		}
		else if `year' == 1998 {
			use "${data}`year'/hh/la_t2.dta", clear
		}
		else if `year' == 1999 {
			use "${data}`year'/hh/la2t.dta", clear
		}
		else {
			local yr = mod(`year',100)
			use "${data}`year'/hh/hh0`yr'_14la_tab2.dta", clear
		}
		
		//Generate two measures of land value: value of all owned land, and value of
		//non-rice land
		replace la30 = . if la30 < 0
		bysort ${idvar}: egen un_landval_all 		= total(la30)
		bysort ${idvar}: egen un_landval_nonrice 	= total(la30) if la24 != 5
		
		collapse (median) un_landval*, by(${idvar})
		gen year = `year' - 1
		
		tempfile `year'
		save ``year''
	}

	use `1997'
	foreach year of numlist 1998/2009 {
		append using ``year''
	}

	merge 1:1 ${idvar} year using `landvaltmp'
	drop _m

	//I'll assume rice land not farmed by the household counts as "non-rice land,"
	//as it can't be correlated with the measurement error in my measure of land
	egen landval_all 		= rowtotal(un_landval_all us_landval_all)
	egen landval_nonrice 	= rowtotal(un_landval_all us_landval_nonrice)


	//Deflate
	forvalues year = 1996/2008 {
		foreach var of varlist un_landval_all un_landval_nonrice us_landval_all us_landval_nonrice landval_all landval_nonrice {
			replace `var' = `var' / ${def`year'} if year == `year'
		}
	}

	tempfile landval
	save `landval'

	/*****************************************************************
	Risk Preferences
	*****************************************************************/

	use "${data}2003/hh/hh03_21hs.dta", clear
		
		
	//The partition of responses is as follows:
	//HS8 == 1 & HS10 == 3 => Highly averse (CRRA > 3.765)
	//HS8 == 1 & HS10 == 1 => Medium-High averse (2 < CRRA <= 3.765)
	//HS8 == 3 & HS9  == 3 => Medium-Low averse (1 < CRRA <= 2)
	//HS8 == 3 & HS9  == 1 => Barely averse (CRRA < 1)
	recode hs9 (3 = 1) (1 = 3)

	gen aversHigh = hs8 == 1 & hs10 == 3
	gen aversMH   = hs8 == 1 & hs10 == 1
	gen aversML   = hs8 == 3 & hs9  == 3
	gen aversLow  = hs8 == 3 & hs9  == 1

	//The people who failed to answer the question - this might
	//be informative
	gen aversfail = hs8*hs9 == . & hs8*hs10 == .

	label var aversHigh "Highly averse (CRRA > 3.765)"
	label var aversMH "Medium-High averse (2 < CRRA <= 3.765)"
	label var aversML "Medium-Low averse (1 < CRRA <= 2)"
	label var aversLow "Barely averse (CRRA <= 1)"
	label var aversfail "No response/unable to respond on aversion questions"

	keep ${idvar} avers*

	tempfile risk
	save `risk'
		
		
		
		
		
	/*****************************************************************
	Non-Farm income
	*****************************************************************/


	/*****************************
	Step 1: Non-Farm revenue
	*****************************/
	//Loop through each year
	foreach year of numlist 1997/2009 {
		if `year' == 1997 {
			use "${data}`year'/hh/j24tb1.dta", clear
			
			//Drop all crop and livestock revenue (i.e. farm revenue)
			drop if in_id <= 5 | (in_id >= 8 & in_id <= 10)
		}
		else if `year' == 1998 {
			use "${data}`year'/hh/in_t1.dta", clear
			
			//Drop all crop and livestock revenue (i.e. farm revenue)
			drop if in_id <= 5 | (in_id >= 8 & in_id <= 10)
		}
		else if `year' == 1999 {
			use "${data}`year'/hh/in1_t.dta", clear
			
			//Drop all crop and livestock revenue (i.e. farm revenue)
			drop if in2_id <= 5 | (in2_id >= 8 & in2_id <= 10)
		}
		else {
			local yr = mod(`year',100)
			use "${data}`year'/hh/hh0`yr'_16in_tab1.dta", clear
			
			
			//Drop all crop and livestock revenue (i.e. farm revenue)
			#delimit;
			drop if 
				number == "A" |
				number == "B" |
				number == "C" |
				number == "D" |
				number == "E" |
				number == "H" |
				number == "I" |
				number == "J";
			#delimit cr
		}
		

		replace in4 = . if in4 < 0
		collapse (sum) nonfarmrev=in4, by(${idvar})
		gen year = `year' - 1
		
		tempfile `year'
		save ``year''
	}


	use `1997'
	foreach year of numlist 1998/2009 {
		append using ``year''
	}


	tempfile nonfarmrev
	save `nonfarmrev'


	/*****************************
	Step 1: Non-Farm expenses (and thus, income)
	*****************************/
	//Loop through each year
	foreach year of numlist 1997/2009 {
		if `year' < 2000 {
			use "${data}`year'/hh/master.dta", clear
			

		}
		else {
			local yr = mod(`year',100)
			use "${data}`year'/hh/hh0`yr'_16in.dta", clear
			
			
		}
		
		
		//Keep just ID and non-farm expenses
		egen nonfarmexp = rowtotal(in10s in10b) 
		keep ${idvar} nonfarmexp
		
		gen year = `year' - 1
		
		tempfile `year'
		save ``year''
	}


	use `1997'
	foreach year of numlist 1998/2009 {
		append using ``year''
	}

	replace nonfarmexp = . if nonfarmexp < 0

	//Failed merges are people with no non-farm revenue
	merge 1:1 ${idvar} year using `nonfarmrev'
	drop _m

	recode nonfarmrev (. = 0)


	gen nonfarminc = nonfarmrev - nonfarmexp

	//Deflate
	forvalues year = 1996/2008 {
		foreach var of varlist nonfarmrev nonfarmexp nonfarminc {
			replace `var' = `var' / ${def`year'} if year == `year'
		}
	}

	tempfile nonfarminc
	save `nonfarminc'



	/*****************************************************************
	Hypothetical: Borrowing
	*****************************************************************/
	foreach year of numlist 1997/2009 {
		if `year' < 2000 {
			use "${data}`year'/hh/master.dta", clear
			

		}
		else {
			local yr = mod(`year',100)
			use "${data}`year'/hh/hh0`yr'_21hs.dta", clear
			
			
		}
			
		//Keep just their first solutions to the question
		keep ${idvar} hs1 hs4
		numlabel _all, add
		
		gen year = `year'-1
		
		tempfile `year'
		save ``year''
		

	}

	use `1997'
	foreach year of numlist 1998/2009 {
		append using ``year''
	}


	foreach var of varlist hs* {
		replace `var' = . if `var' < 0
	}

	//Use savings
	gen sav_2k 		= hs1 == 1 if !mi(hs1)
	gen sav_20k		= hs4 == 1 if !mi(hs4)

	//Borrow from relative
	gen rel_2k 		= hs1 >=23 & hs1 <= 35		if !mi(hs1)
	gen rel_20k 	= hs4 >=23 & hs4 <= 35		if !mi(hs4)

	tempfile hypborr
	save `hypborr'

	/*****************************************************************
	Merge
	*****************************************************************/


	use "${path}dta/sepfact.dta", clear
	merge m:1 ${idvar} using `risk'
	tab _m
	drop _m

	merge 1:1 ${idvar} year using `landval'
	drop _m

	merge 1:1 ${idvar} year using `nonfarminc'
	drop _m

	merge 1:1 ${idvar} year using `hypborr'
	drop _m


	gen share = log(intermed/ricerev)
	gen logKunfull = log(Kfull - intermed)
	gen logM = log(intermed)
	gen loglandval = log(landval_nonrice + 1)

	//Coefficient of risk-aversion
	gen gamma = 0*aversLow + 1*aversML + 2*aversMH + 3.765*aversHigh if aversfail == 0



	drop hhid
	egen hhid = group($idvar)
	xtset hhid year
	sort hhid year


end

/*****************************************************************
******************************************************************
Run Tests
******************************************************************
*****************************************************************/
local runtests 0
local dosharetest 1
local dowidstat 1

local widreps 500

if "`runtests'" == "1" {


	if "`dosharetest'" == "1" {
		cap postclose testtable
		postfile testtable str10 stat double pval using "dta/emp_results/testtable.dta", replace
	}
	
	if "`dowidstat'" == "1" {
		cap postclose widtable
		postfile widtable str25 sample str25 specification double widstat double pct10 double pct5 double pct1 using "dta/emp_results/widtable.dta", replace
		
		//$20 bill in my pocket:
		set seed 49240021
	}
	
	

	/*****************************************************************
	Chile
	*****************************************************************/

	use "dta/ChileAnalysis2.dta", clear

	foreach ind in 311 321 322 331 381 {
		preserve
			formdata `ind'


			//Make variables with friendlier names
			gen t = year
			xtset id t

			gen y = logRGO
			gen k = logK
			gen l = logL
			gen m = logRI

			//Get higher-order terms
			product k l m
			
			
			/********
			Share test
			********/
			if "`dosharetest'" == "1" {
				gen logSHARE = si if si != -1000

				//Test with lagged inputs
				sharetest logSHARE k l m, test( l2.( c.k##c.m ) ) lagtest deg(${deg}) lagdeg(1) con(i.t)
				post testtable ("chlag`ind'") (`r(pval)')
				
				//Test with short-term assets
				gen ihs_asset = asinh(stassets)
				gen noasset = stassets==0
				sharetest logSHARE k l m, test( l.(ihs_asset noasset) ) deg(${deg}) con(i.t)
				post testtable ("chsta`ind'") (`r(pval)')
			}
				
			
			/********
			WID test
			********/			
			if "`dowidstat'" == "1" {
				foreach spec in simple  {
					tempfile prewid
					save `prewid'
					
					widstat_`spec'
					bspctiles_`spec' `widreps' "dta/emp_results/`spec'_bswid_chile`ind'.dta"
					
					local postcom
					foreach stat of varlist widstat_iv widstatp_1 widstatp_2 widstatp_3 {
						sum `stat'
						local postcom `postcom' (`r(mean)')
					}
					post widtable ("chile `ind'") ("`spec'") `postcom'
					
					use `prewid', clear
				}
			}
		
		restore
	}
		
	/*****************************************************************
	Colombia
	*****************************************************************/
	colraw

	foreach ind of numlist 311 321 322 331 381 {
		preserve
			formdatacol `ind'
			
			
			//Make variables with friendlier names
			gen t = year
			xtset id t
			
			gen y = logRGO
			gen k = logK
			gen l = logL
			gen m = logRI
			
			//Get higher-order terms
			product k l m
			
			/********
			Share test
			********/
			if "`dosharetest'" == "1" {
				//Test with lagged inputs
				gen logSHARE = si if si != -1000
				sharetest logSHARE k l m, test( l2.( c.k##c.m ) ) lagtest deg(${deg}) lagdeg(1) con(i.t)
				post testtable ("colag`ind'") (`r(pval)')
				
				//Test with end-of-year materials
				sharetest logSHARE k l m, test( l.savk ) deg(${deg})  con(i.t)
				post testtable ("colym`ind'") (`r(pval)')
			}
				
			
			if "`dowidstat'" == "1" {
				foreach spec in simple  {
					tempfile prewid
					save `prewid'
					
					widstat_`spec'
					bspctiles_`spec' `widreps' "dta/emp_results/`spec'_bswid_colombia`ind'.dta"
					
					local postcom
					foreach stat of varlist widstat_iv widstatp_1 widstatp_2 widstatp_3 {
						sum `stat'
						local postcom `postcom' (`r(mean)')
					}
					post widtable ("colombia `ind'") ("`spec'")  `postcom'
					
					use `prewid', clear
				}
			}
		restore
	}


	if "`dowidstat'" == "1" {
		postclose widtable
	}


	/*****************************************************************
	Thailand
	*****************************************************************/
	if "`dosharetest'" == "1" {
		formdatathai

		sharetest share logKunfull logLfull logT_rice logM, test( l2.logM ) deg(${deg}) lagdeg(1) con(i.year) lagtest
		post testtable ("thlag") (`r(pval)')

		sharetest share logKunfull logLfull logT_rice logM, test( gamma ) deg(${deg}) lagdeg(1) con(i.year)
		post testtable ("thrisk") (`r(pval)')

		sharetest share logKunfull logLfull logT_rice logM, test( factcon credcon ) deg(${deg}) lagdeg(1) con(i.year)
		post testtable ("thcon") (`r(pval)')

		sharetest share logKunfull logLfull logT_rice logM, test( sav_2k sav_20k rel_2k rel_20k ) deg(${deg}) lagdeg(1) con(i.year)
		post testtable ("thsav") (`r(pval)')

		sharetest share logKunfull logLfull logT_rice logM, test( gamma factcon credcon sav_2k sav_20k rel_2k rel_20k ) deg(${deg}) lagdeg(1) con(i.year)
		post testtable ("thall") (`r(pval)')


		postclose testtable
	}
	
	

}





/*****************************************************************
******************************************************************
Form Tables
******************************************************************
*****************************************************************/



/*****************************************************************
Share Test
*****************************************************************/
use "dta/emp_results/testtable.dta", clear
gen pval_format = string(pval,"%9.3f")

local keylist  
forvalues i=1/`=_N' {
	local tmp = stat in `i'
	local `tmp' = pval_format in `i'
	local keylist `keylist' `tmp'
}



//Base file:
tempfile desc0
filefilter "templates/sharetest.txt" "`desc0'", from(" ") to(" ")

//Loop
local onfile = 0
foreach key in `keylist' {
	local tmp = `onfile'+1
	tempfile desc`tmp'
	filefilter "`desc`onfile''" "`desc`tmp''", from("`key'") to("``key''")
	local onfile = `tmp'
}

//The last temporary file we made is the final version
copy "`desc`tmp''" "tables/sharetest.tex", replace


/*****************************************************************
WIDstat Test
*****************************************************************/
use "dta/emp_results/widtable.dta", clear
gen stub = substr(sample,1,2) + substr(sample,-3,3)
foreach var of varlist widstat pct10 pct5 pct1 {
	rename `var' `var'_num
	gen `var' = string(`var'_num, "%9.2f")
}


foreach p in 10 5 1 {
	replace widstat = widstat + "*" if widstat_num > pct`p'_num
}

local keylist  
forvalues i=1/`=_N' {
	local stub = stub in `i'
	foreach var of varlist widstat pct10 pct5 pct1 {
		local tmp 	= "`stub'`var'"
		local `tmp' = `var' in `i'
		local keylist `keylist' `tmp'
	}
	
}


//Base file:
tempfile desc0
filefilter "templates/widtest.txt" "`desc0'", from(" ") to(" ")

//Loop
local onfile = 0
foreach key in `keylist' {
	local tmp = `onfile'+1
	tempfile desc`tmp'
	filefilter "`desc`onfile''" "`desc`tmp''", from("[`key']") to("``key''")
	local onfile = `tmp'
}

//The last temporary file we made is the final version
copy "`desc`tmp''" "tables/widtable.tex", replace
